#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static void worker(pid_t pid)
{
    printf("grand-child: %d\n", pid);
    sleep(150);
}

int main()
{
    pid_t pid = 0;
    int status = 0;

    if ((pid = fork()) < 0) {
        printf("fork error\n");
    } else if (pid == 0) {
        int i = 0;
        for (i = 0; i < 5; i++) {
            if ((pid = fork()) == 0) {
                worker(getpid());
                break;
            }
        }

        sleep(60);
        printf("child(%d) is over ...\n", getpid());
    } else {
        printf("wait child = %d\n", pid);
        sleep(120);
        while (waitpid(pid, &status, 0) == pid) {
            printf("Normal - child: %d, status: %x\n", pid, status);
        }
    }

    return 0;
}
